import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import '../model/AccountingModel.dart';
import '../global/GlobalTheme.dart';

class AccountingViewModel extends ChangeNotifier {
  List _list = [];
  int _month = DateTime.now().month;
  double _expend = 0; //支出
  double _income = 0; //收入

  get getExpend {
    return _expend;
  }

  void setExpend(double val) {
    _expend = val;
    notifyListeners();
  }

  get getIncome {
    return _income;
  }

  void setIncome(double val) {
    _income = val;
    notifyListeners();
  }

  List get getList {
    return _list;
  }

  void setList(List list) {
    _list = list;
    notifyListeners();
  }

  get getMonth {
    // if(_month == null) _month = DateTime.now().month;
    return _month;
  }

  void setMonth(int month) {
    _month = month;
    notifyListeners();
  }

  void accountingHistory() async {
    setList([]);  // 查询时先制空
    Response res = await getAccountHistory(
        DateTime.now().year.toString() +
            (getMonth < 10 ? "0" + getMonth.toString() : getMonth.toString())
    );
    if (res.data["success"]) {
      setList(res.data["data"]["data"]);
      setExpend(res.data["data"]["expenditure"]);
      setIncome(res.data["data"]["income"]);
    } else {
      EasyLoading.showError(res.data['msg']);
    }
  }

  void delete(String id) async {
    Response res = await deleteAccountingList(id);
    // print(result);
    if (res.data["success"]) {
      accountingHistory();
    } else {
      EasyLoading.showError(res.data['msg']);
    }
  }

}